<?php

namespace app\admin\controller\ykjp\purchase;

use app\common\controller\Backend;
use think\Db;

/**
 * 退货审核管理
 *
 * @icon fa fa-circle-o
 */
class RetReview extends Backend {

    protected $model = null;
    protected $distinguish = true;
    protected $wherename = "";

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\ykjp\purchase\Retire;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 查看
     */
    public function index() {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $this->wherename = "retire.firmid";
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['supplier'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['supplier'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {

                $row->getRelation('supplier')->visible(['name']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 审核
     */
    public function reviewRet($ids = null) {
        if ($ids) {
            $type = $this->request->param("type");

            if (!$type) {
                $this->error(__('Parameter %s can not be empty', 'type'));
            }
            $all = $this->model->where("id", "in", $ids)->where("status", "0")->where("firmid", $this->auth->firmid)->select();
            $resProModel = new \app\admin\model\ykjp\purchase\Retprodcut;
            if ($all) {
                if ($type == "yes" || $type = "no")
                    if ($type == "yes") {
                        $data = array("updatetime" => time(), "status" => "1");
                        $productData = $data;
                    } else {
                        $reason = $this->request->param("reason");
                        if (!$reason) {
                            $this->error(__("Reason cannot be empty"));
                        }
                        $reason = strip_tags($reason);
                        $data = array("updatetime" => time(), "status" => "2", "reason" => $reason);
                        $productData = array("updatetime" => time(), "status" => "2");
                    }
                $count = 0;
                foreach ($all as $key => $val) {
                    $up = $this->model->where("id", $val['id'])->update($data);
                    $count += $up;
                    if ($up) {
                        $upProdcut = $resProModel->where("firmid", $this->auth->firmid)->where("retire_id", $val['id'])->update($productData);
                        if (!$upProdcut) {
                            $update = $this->model->where("id", $val['id'])->update(array("status" => "0"));
                            $count -= $update;
                        }
                    }
                }
                if ($count) {
                    $this->success();
                }
            }
            $this->error(__('Operation fail'));
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    /**
     * 详情
     */
    public function detail($ids = "") {
        if ($ids) {
            $detail = $this->model->with(['supplier'])->where("retire.id", $ids)->where("retire.firmid", $this->auth->firmid)->select();
            $resProModel = new \app\admin\model\ykjp\purchase\Retprodcut;
            $product = [];
            if ($detail) {
                $product = $resProModel->with(['products'])->where("retprodcut.firmid", $this->auth->firmid)->where("retire_id", $ids)->select();
                foreach ($product as $key => $val) {
                    $pro = json_decode($val['products']['prop']);
                    $val['products']['prop'] = $this->model->object_array($pro);
                }
            }
            $this->assign("detail", $detail);
            $this->assign("product", $product);
            return $this->view->fetch();
        }
        $this->error(__('Parameter %s can not be empty', "ids"), "ykjp/purchase/RetReview/index");
    }

}
